from datetime import date, timedelta

from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.goods.models import GoodsVisitCount
from apps.meiduo_admin.serializers.count_serializer import UserModelSerializer
from apps.user.models import User


# 日活跃用户统计
class UserDailyActiveCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取活跃用户总数
        count = User.objects.filter(last_login__gte=now_date).count()
        return Response({
            'count': count,
            'date': now_date
        })


# 日下单用户量统计
class UserDailyOrderCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日下单用户量 orders__create_time 订单创建时间
        count = User.objects.filter(orderinfo__create_time__gte=now_date).count()
        return Response({
            'count': count,
            'date': now_date,
        })


# 月增用户统计
class UserMonthCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取一个月前的日期
        start_date = now_date - timedelta(days=30)
        # 创建空列表保存每天用户量
        date_list = []
        for i in range(30):
            # 循环遍历获取当天日期
            index_date = start_date + timedelta(days=i)
            # 指定下一天日期
            cur_date = start_date + timedelta(days=i + 1)
            # 查询条件达与当前日期index_date，小于明天日期的用户cue_date，得到当天用户量
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=cur_date).count()
            date_list.append({
                'count': count,
                'date': index_date
            })
        return Response(date_list)


# 日增用户量统计
class UserDayCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        count = User.objects.filter(date_joined__gte=now_date).count()
        return Response({
            'count': count,
            'date': now_date,
        })


# 用户总量统计
class UserCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期
        now_date = date.today()
        # 获取用户总量
        count = User.objects.all().count()
        return Response({
            'count': count,
            'date': now_date
        })


# 日分类商品访问量统计
class UserGoodsView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期
        now_date = date.today()
        # 获取当天分类访问数量
        goods = GoodsVisitCount.objects.filter(date__gte=now_date)
        ser = UserModelSerializer(goods, many=True)
        return Response(ser.data)
